package com.example.chap2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

public class SimpleCtl2 extends AppCompatActivity implements View.OnClickListener,View.OnLongClickListener {
    private TextView tv_bbs;
    private TextView tv_control;
    private String[] mChatStr={"你吃饭了吗？","今天天气真好啊。","我中奖了！","我们去看电影吧","晚上干什么好呢？"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_ctl2);
        tv_control=findViewById(R.id.tv_control);
        tv_control.setOnClickListener(this);//监听单击
        tv_control.setOnLongClickListener(this);//监听长按
        tv_bbs=findViewById(R.id.tv_bbs);
        tv_bbs.setOnClickListener(this);
        tv_bbs.setOnLongClickListener(this);
        tv_bbs.setGravity(Gravity.LEFT|Gravity.BOTTOM);//控件内文字堆砌方式：靠左且靠右
        tv_bbs.setLines(8);
        tv_bbs.setMaxLines(8);
        tv_bbs.setMovementMethod(new ScrollingMovementMethod());//设置滚动方式移动
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.tv_control || view.getId()==R.id.tv_bbs){
//            生成一个0到4之间的随机数
            int random=(int)(Math.random()*10)%5;
            String newStr=String.format("%s\n%s %s",tv_bbs.getText().toString(), DateUtil.getNowTime(),mChatStr[random]);
            tv_bbs.setText(newStr);
        }
    }

    @Override
    public boolean onLongClick(View view) {
        if (view.getId()==R.id.tv_control || view.getId()==R.id.tv_bbs){
            tv_bbs.setText("");
        }
        return true;
    }
}